package com.it.oauth2.custom.service;



import com.it.auth.custom.user.Auth2User;
import com.it.oauth2.server.User;
import com.it.oauth2.server.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @program: learning
 * @description: 自定义userdetailservice实现类
 * @author: Lefnmg
 * @create: 2018-11-19 21:40
 **/
@Service("auth2UserService")
public class Auth2UserService implements UserDetailsService {

    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserServiceImpl userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ArrayList<SimpleGrantedAuthority> objects = new ArrayList<>();
        objects.add(new SimpleGrantedAuthority("admin"));
        User user = userService.findByName(username);
        Auth2User auth2User = new Auth2User();
        auth2User.setUsername(user.getUsername());
        auth2User.setPassword(passwordEncoder.encode(user.getPassword()));
        auth2User.setAuthorities(objects);
        return auth2User;
    }
}
